//
//  HXMakeFoodVC.m
//  ZCooking
//
//  Created by 韩旭 on 16/9/12.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXMakeFoodVC.h"
#import "HX_Macro.h"
#import "HXMakeFoodTCell.h"
#import "HXMaterialModel.h"
#import "HXFoodMaterialModel.h"
#import "HXMakeImageCell.h"

@interface HXMakeFoodVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *foodMake;
@property (nonatomic, strong) UIImageView *headerImage;
@end
@implementation HXMakeFoodVC
- (NSMutableArray *)foodMake {
    if (!_foodMake) {
        _foodMake = [NSMutableArray array];
    }
    return _foodMake;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatHeaderView];
    [self tableView];
    [self getDishesMakeFoodWithModelDishes_id:self.dishes_id];
}
- (void)creatHeaderView {
    self.headerImage = [[UIImageView alloc] init];
    _headerImage.backgroundColor = [UIColor whiteColor];
    _headerImage.HX_width = SCREEN_WIDTH;
    _headerImage.HX_height = 200;
    _headerImage.HX_x = 0;
    _headerImage.HX_y = 0;
}
- (void)getDishesMakeFoodWithModelDishes_id:(NSString *)dishes_id {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"methodName=DishesMaterial&dishes_id=%@&version=4.40&", dishes_id];
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        HXFoodMaterialModel *material = [HXFoodMaterialModel ModelWithDic:dicData];
        [self.headerImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", material.material_image]]];
        [self.foodMake addObject:material];
        [_tableView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 170) style:0];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = _headerImage;
        [self.view addSubview:_tableView];
        [_tableView registerNib:[UINib nibWithNibName:@"HXMakeFoodTCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HXMakeFoodTCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"HXMakeImageCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HXMakeImageCell"];
    }
    return _tableView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      HXFoodMaterialModel *foodMake = [self.foodMake firstObject];
    if (section == 0) {
        return foodMake.material.count;
    } else {
        return [foodMake.spices count];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     HXFoodMaterialModel *food = [self.foodMake firstObject];
    if (indexPath.section == 0) {
        HXFoodMaterialModel *food = [self.foodMake firstObject];
        HXMakeFoodTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HXMakeFoodTCell"];
        cell.foodLabel.text = [food.material[indexPath.row] material_name];
        cell.weight.text = [food.material[indexPath.row] material_weight];
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    } else {
        HXMakeImageCell *cell= [tableView dequeueReusableCellWithIdentifier:@"HXMakeImageCell"];
        [cell.headerImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [food.spices[indexPath.row] image]]]];
        cell.name.text = [food.spices[indexPath.row] title];
        cell.backgroundColor = COLORAUTO;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 40;
    } else {
        return 200;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end